<title>Watching Fireworks is Fun</title><link href="http://hzoi.com/tomorrow.css" rel="stylesheet"><div class="ui existing segment"><pre><code><span class="pl-cm">/*</span>
<span class="pl-cm">50 3 1</span>
<span class="pl-cm">49 1 1</span>
<span class="pl-cm">26 1 4</span>
<span class="pl-cm">6 1 10</span>
<span class="pl-cm">*/</span>
<span class="pl-cp">#include</span><span class="pl-cpf">&lt;bits/stdc++.h&gt;</span><span class="pl-cp"></span>
<span class="pl-k">using</span> <span class="pl-k">namespace</span> <span class="pl-n">std</span><span class="pl-p">;</span>
<span class="pl-k">const</span> <span class="pl-kt">int</span> <span class="pl-n">maxn</span><span class="pl-o">=</span><span class="pl-mf">15e4</span><span class="pl-o">+</span><span class="pl-mi">10</span><span class="pl-p">;</span>
<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">dp</span><span class="pl-p">[</span><span class="pl-mi">2</span><span class="pl-p">][</span><span class="pl-n">maxn</span><span class="pl-p">],</span><span class="pl-n">a</span><span class="pl-p">,</span><span class="pl-n">b</span><span class="pl-p">,</span><span class="pl-n">t</span><span class="pl-p">,</span><span class="pl-n">last_t</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">,</span><span class="pl-n">delta</span><span class="pl-p">,</span><span class="pl-n">q</span><span class="pl-p">,</span><span class="pl-n">m</span><span class="pl-p">,</span><span class="pl-n">d</span><span class="pl-p">,</span><span class="pl-n">n</span><span class="pl-p">,</span><span class="pl-n">line</span><span class="pl-o">=</span><span class="pl-mi">0</span><span class="pl-p">;;</span>
<span class="pl-kt">int</span> <span class="pl-nf">main</span><span class="pl-p">(){</span>
	<span class="pl-n">scanf</span><span class="pl-p">(</span><span class="pl-s">&quot;%lld%lld%lld&quot;</span><span class="pl-p">,</span><span class="pl-o">&amp;</span><span class="pl-n">n</span><span class="pl-p">,</span><span class="pl-o">&amp;</span><span class="pl-n">m</span><span class="pl-p">,</span><span class="pl-o">&amp;</span><span class="pl-n">d</span><span class="pl-p">);</span> 
	<span class="pl-k">while</span><span class="pl-p">(</span><span class="pl-n">m</span><span class="pl-o">--</span><span class="pl-p">){</span>
		<span class="pl-n">scanf</span><span class="pl-p">(</span><span class="pl-s">&quot;%lld%lld%lld&quot;</span><span class="pl-p">,</span><span class="pl-o">&amp;</span><span class="pl-n">a</span><span class="pl-p">,</span><span class="pl-o">&amp;</span><span class="pl-n">b</span><span class="pl-p">,</span><span class="pl-o">&amp;</span><span class="pl-n">t</span><span class="pl-p">);</span>
		<span class="pl-n">delta</span><span class="pl-o">=</span><span class="pl-p">(</span><span class="pl-n">t</span><span class="pl-o">-</span><span class="pl-n">last_t</span><span class="pl-p">)</span><span class="pl-o">*</span><span class="pl-n">d</span><span class="pl-p">,</span> <span class="pl-n">last_t</span><span class="pl-o">=</span><span class="pl-n">t</span><span class="pl-p">;</span>
		<span class="pl-n">deque</span><span class="pl-o">&lt;</span><span class="pl-kt">int</span><span class="pl-o">&gt;</span> <span class="pl-n">q</span><span class="pl-p">;</span>
		<span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">i</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">,</span><span class="pl-n">j</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">&lt;=</span><span class="pl-n">n</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">){</span>
			<span class="pl-k">for</span><span class="pl-p">(;</span><span class="pl-n">j</span><span class="pl-o">&lt;=</span><span class="pl-n">i</span><span class="pl-o">+</span><span class="pl-n">delta</span><span class="pl-o">&amp;&amp;</span><span class="pl-n">j</span><span class="pl-o">&lt;=</span><span class="pl-n">n</span><span class="pl-p">;</span><span class="pl-n">j</span><span class="pl-o">++</span><span class="pl-p">){</span>
				<span class="pl-k">while</span><span class="pl-p">(</span><span class="pl-o">!</span><span class="pl-n">q</span><span class="pl-p">.</span><span class="pl-n">empty</span><span class="pl-p">()</span><span class="pl-o">&amp;&amp;</span><span class="pl-n">dp</span><span class="pl-p">[</span><span class="pl-n">line</span><span class="pl-p">][</span><span class="pl-n">q</span><span class="pl-p">.</span><span class="pl-n">back</span><span class="pl-p">()]</span><span class="pl-o">&lt;=</span><span class="pl-n">dp</span><span class="pl-p">[</span><span class="pl-n">line</span><span class="pl-p">][</span><span class="pl-n">j</span><span class="pl-p">])</span><span class="pl-n">q</span><span class="pl-p">.</span><span class="pl-n">pop_back</span><span class="pl-p">();</span>
				<span class="pl-n">q</span><span class="pl-p">.</span><span class="pl-n">push_back</span><span class="pl-p">(</span><span class="pl-n">j</span><span class="pl-p">);</span>
			<span class="pl-p">}</span>
			<span class="pl-k">while</span><span class="pl-p">(</span><span class="pl-o">!</span><span class="pl-n">q</span><span class="pl-p">.</span><span class="pl-n">empty</span><span class="pl-p">()</span><span class="pl-o">&amp;&amp;</span><span class="pl-n">q</span><span class="pl-p">.</span><span class="pl-n">front</span><span class="pl-p">()</span><span class="pl-o">&lt;</span><span class="pl-n">i</span><span class="pl-o">-</span><span class="pl-n">delta</span><span class="pl-p">)</span><span class="pl-n">q</span><span class="pl-p">.</span><span class="pl-n">pop_front</span><span class="pl-p">();</span>
			<span class="pl-n">dp</span><span class="pl-p">[</span><span class="pl-o">!</span><span class="pl-n">line</span><span class="pl-p">][</span><span class="pl-n">i</span><span class="pl-p">]</span><span class="pl-o">=</span><span class="pl-n">dp</span><span class="pl-p">[</span><span class="pl-n">line</span><span class="pl-p">][</span><span class="pl-n">q</span><span class="pl-p">.</span><span class="pl-n">front</span><span class="pl-p">()]</span><span class="pl-o">+</span><span class="pl-n">b</span><span class="pl-o">-</span><span class="pl-n">abs</span><span class="pl-p">(</span><span class="pl-n">i</span><span class="pl-o">-</span><span class="pl-n">a</span><span class="pl-p">);</span>
		<span class="pl-p">}</span>
		<span class="pl-n">line</span><span class="pl-o">=!</span><span class="pl-n">line</span><span class="pl-p">;</span>
	<span class="pl-p">}</span>
	<span class="pl-kt">long</span> <span class="pl-kt">long</span> <span class="pl-n">ans</span><span class="pl-o">=</span><span class="pl-n">dp</span><span class="pl-p">[</span><span class="pl-n">line</span><span class="pl-p">][</span><span class="pl-mi">1</span><span class="pl-p">];</span>
	<span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">i</span><span class="pl-o">=</span><span class="pl-mi">2</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">&lt;=</span><span class="pl-n">n</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">){</span>
		<span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-n">dp</span><span class="pl-p">[</span><span class="pl-n">line</span><span class="pl-p">][</span><span class="pl-n">i</span><span class="pl-p">]</span><span class="pl-o">&gt;</span><span class="pl-n">ans</span><span class="pl-p">)</span><span class="pl-n">ans</span><span class="pl-o">=</span><span class="pl-n">dp</span><span class="pl-p">[</span><span class="pl-n">line</span><span class="pl-p">][</span><span class="pl-n">i</span><span class="pl-p">];</span>
	<span class="pl-p">}</span>
	<span class="pl-n">printf</span><span class="pl-p">(</span><span class="pl-s">&quot;%lld&quot;</span><span class="pl-p">,</span><span class="pl-n">ans</span><span class="pl-p">);</span>
<span class="pl-p">}</span>
</cpde></pre></div>